# -*- encoding: utf-8 -*-
"""
File Name: launch
Description:
Author : 'lirui'
date: 2/6/2024
Contact: marjey_lee@163.com
Change Activity:
2/6/2024:
"""
import uvicorn
from starlette.middleware.cors import CORSMiddleware

import ll
from ll.restful.fast_api_util.init_app import init_fastapi_app
from projects.monitoring.alarms_manager.services.agent_health_check import AgentHealthCheck
from projects.monitoring.alarms_manager.services.load_redis_info import RedisAlarmManager
from projects.monitoring.alarms_manager.services.subscriber_manager import SUBSCRIBER_MANAGER

_APP = init_fastapi_app('alarm manager', 'alarm manager')

_APP.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

SUBSCRIBER_MANAGER.create_all_subscribers()

REDIS_ALARM_MANAGER = RedisAlarmManager()


@_APP.get('/get_current_warning_alarms')
def get_current_warning_alarms():
    return REDIS_ALARM_MANAGER.get_current_warning_alarms()


@_APP.get('/get_all_alarms')
def get_all_alarms():
    return REDIS_ALARM_MANAGER.get_all_current_alarms()


@_APP.get('/get_agent_status')
def get_all_alarms():
    return AgentHealthCheck()()


def _main():
    ll.get_logger().info('Launch server.')
    uvicorn.run(_APP, host="0.0.0.0", port=8080, log_level='warning')  # start fast api server


if __name__ == "__main__":
    _main()
